package com.shorturi.uri;

import com.robert.vesta.service.impl.IdServiceImpl;
import com.robert.vesta.service.impl.provider.PropertyMachineIdProvider;
import com.robert.vesta.service.intf.IdService;
import org.elasticsearch.client.Client;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.util.UUID;

/**
 * @author sola
 */
@Configuration
@EnableElasticsearchRepositories
public class ConfigurationBean {

    @Bean
    @Primary
    public NodeClientFactoryBean clientLocal() {

        NodeClientFactoryBean bean = new NodeClientFactoryBean(true);
        bean.setClusterName(UUID.randomUUID().toString());
        bean.setEnableHttp(false);
        bean.setPathData("target/elasticsearchTestData");
        bean.setPathHome("src/test/resources/test-home-dir");
        return bean;
    }

    @Bean
    public ElasticsearchTemplate elasticsearchTemplate(Client clientPrimary) {
        return new ElasticsearchTemplate(clientPrimary);
    }

    @Bean
    public PropertyMachineIdProvider propertyMachineIdProvider() {
        final PropertyMachineIdProvider propertyMachineIdProvider = new PropertyMachineIdProvider();
        propertyMachineIdProvider.setMachineId(1);
        return propertyMachineIdProvider;
    }

    @Bean
    public IdService idService(final PropertyMachineIdProvider propertyMachineIdProvider) {
        final IdServiceImpl idService = new IdServiceImpl();
        idService.setMachineIdProvider(propertyMachineIdProvider);
        idService.init();
        return idService;
    }

}
